<template>
  <div>
    <div class="mg-10">
      <el-input
        style="width: 150px"
        suffix-icon="el-icon-search"
        v-model="username"
        placeholder="请输入用户名"
      ></el-input>
      <el-input
        style="width: 150px"
        suffix-icon="el-icon-search"
        v-model="nickname"
        placeholder="请输入昵称"
      ></el-input>
      <el-input
        style="width: 200px"
        suffix-icon="el-icon-mobile-phone"
        v-model="phone"
        placeholder="请输入手机号"
      ></el-input>
      <el-input
        style="width: 150px"
        suffix-icon="el-icon-message"
        v-model="email"
        placeholder="请输入邮箱"
      ></el-input>
      <el-input
        style="width: 150px"
        suffix-icon="el-icon-message"
        v-model="address"
        placeholder="请输入地址"
      ></el-input>
      <!-- <el-select v-model="roleId" label="请选择权限">
        <el-option label="请选择" value="">请选择</el-option>
        <el-option label="用户" value="1">用户</el-option>
        <el-option label="管理员" value="2">管理员</el-option>
      </el-select> -->

      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
    </div>

    <div class="mg-10">
      <el-button type="primary" @click="handleAdd"
        >新增<i class="el-icon-circle-plus-outline"></i
      ></el-button>

      <el-popconfirm
        class="ml-5"
        confirm-button-text="确定"
        cancel-button-text="我再想想"
        icon="el-icon-info"
        icon-color="red"
        title="您确定批量删除这些用户吗？"
        @confirm="delBatch"
      >
        <el-button type="danger" slot="reference"
          >批量删除<i class="el-icon-remove-outline"></i
        ></el-button>
      </el-popconfirm>

      <el-upload
        class="upload-demo"
        action="http://localhost:9090/user/import" style="display:inline-block"
        accept="xlsx" :on-success="handleExcelImportSucess">
      <el-button type="primary" class="ml-5"
        >导入<i class="el-icon-bottom"></i
      ></el-button>
      </el-upload>
      <el-button type="primary" @click="exp" class="ml-5">导出<i class="el-icon-top"></i></el-button>
    </div>

    <!--border stripe,表示加上斑马线-->
    <!--header-row-class-name="headerBg",表示设置表头颜色样式，headerBg为一个style样式类-->
    <el-table
      :data="tableData"
      border
      stripe
      :header-cell-class-name="headerBg"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80"> </el-table-column>
      <el-table-column prop="username" label="用户名" width="140">
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" width="120">
      </el-table-column>
      <el-table-column prop="phone" label="手机号"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
      <el-table-column prop="role" label="角色">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.role === 'ROLE_ADMIN'">管理员</el-tag>
          <el-tag type="warning" v-if="scope.row.role === 'ROLE_SUPER_ADMIN'">超级管理员</el-tag>
          <el-tag type="success" v-if="scope.row.role === 'ROLE_COMMON'">用户</el-tag>
          <el-tag type="primary" v-if="scope.row.role === 'ROLE_TEAM_ADMIN'">球队管理员</el-tag>
          <el-tag type="primary" v-if="scope.row.role === 'ROLE_PLAYER_ADMIN'">球员管理员</el-tag>
          <el-tag type="primary" v-if="scope.row.role === 'ROLE_GAME_ADMIN'">球赛管理员</el-tag>
          <el-tag type="primary" v-if="scope.row.role === 'ROLE_FILE_ADMIN'">文件管理员</el-tag>
        </template>
      </el-table-column>
      <el-table-column lable="操作">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)"
            >编辑 <i class="el-icon-edit"></i
          ></el-button>
          <el-popconfirm
            class="ml-5"
            confirm-button-text="确定"
            cancel-button-text="我再想想"
            icon="el-icon-info"
            icon-color="red"
            title="您确定删除这个用户吗？"
            @confirm="handledelete(scope.row.id)"
          >
            <el-button type="danger" slot="reference"
              >删除 <i class="el-icon-remove-outline"></i
            ></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!--添加用户-->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="40%">
      <el-form label-width="80px" size="small" :model="users">
        <el-form-item label="用户名称">
          <el-input v-model="users.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select clearable v-model="users.role" placeholder="请选择角色" style="width: 100%">
            <el-option v-for="item in roles" :key="item.name" :label="item.name" :value="item.flag"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户昵称">
          <el-input v-model="users.nickname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="手机号">
          <el-input v-model="users.phone" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="邮箱">
          <el-input v-model="users.email" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="地址">
          <el-input v-model="users.address" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </div>
    </el-dialog>

    <!--修改用户-->
    <el-dialog
      title="用户信息"
      :visible.sync="dialogFormEditVisible"
      width="40%"
    >
      <el-form label-width="80px" size="small" v-model="Editusers">
        <el-form-item label="用户名称">
          <el-input v-model="Editusers.username" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="角色">
          <el-select clearable v-model="Editusers.role" placeholder="请选择角色" style="width: 100%">
            <el-option v-for="item in roles" :key="item.name" :label="item.name" :value="item.flag"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="用户昵称">
          <el-input v-model="Editusers.nickname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="手机号">
          <el-input v-model="Editusers.phone" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="邮箱">
          <el-input v-model="Editusers.email" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="地址">
          <el-input v-model="Editusers.address" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: 'User',
  data(){
    return{
      tableData: [],
      total: 0, //总数
      current: 1, //当前页
      size: 5, //每页数量
      username: "", //用户名输入框
      nickname: "", //昵称输入框
      phone: "", //手机号输入框
      email: "", //邮箱输入框
      address: "", //地址输入框
      dialogFormVisible: false, //添加弹窗默认关闭
      dialogFormEditVisible: false, //修改弹窗默认关闭
      multipleSelection: "", //批量删除选择的
      users: {}, //添加用户form表
      Editusers: {}, //修改用户form表
      headerBg: "headerBg",
      roles:[]
    }
  },
  created() {
    this.reset(), this.load();
  },
  methods:{
     //列表展示、搜索
     load() {
      //请求分页查询数据
      request
        .get("/user/PageUser/", {
          params: {
            current: this.current,
            limit: this.size,
            username: this.username,
            nickname: this.nickname,
            phone: this.phone,
            email: this.email,
            address: this.address,
          },
        })
        .then((res) => {
          console.log(res.total);
          this.tableData = res.pageUsers.records;
          this.total = res.total;
        });
        //获取角色
        this.request.get("/role/findall").then(res => {
        console.log(res.data)
        this.roles = res.data
      })
    },
    

    //分页每页大小
    handleSizeChange(size) {
      console.log(size);
      this.size = size;
      this.load();
    },
    //分页第几页
    handleCurrentChange(current) {
      console.log(current);
      this.current = current;
      this.load();
    },
    //重置搜索框
    reset() {
      (this.username = ""),
        (this.nickname = ""),
        (this.phone = ""),
        (this.email = ""),
        (this.address = ""),
        this.load();
    },
    //点击添加按钮
    handleAdd() {
      this.dialogFormVisible = true;
      this.users = {};
    },
    //点击编辑按钮
    handleEdit(row) {
      this.Editusers = row;
      this.dialogFormEditVisible = true;
    },
    //删除单个用户
    handledelete(id) {
      request.delete("/user/deleteUser/" + id).then((res) => {
        if (res) {
          this.$message.success("删除成功");
          this.load();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    //checkbox选择框
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = val;
    },
    //批量删除
    delBatch() {
      let ids = this.multipleSelection.map((v) => v.id); // [{},{},{}] = >[1,2,3]
      request.delete("/user/batchDelete/", { data: ids }).then((res) => {
        if (res) {
          this.$message.success("删除成功");
          this.load();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    //添加用户
    addUser() {
      request.post("/user/addUser", this.users).then((res) => {
        console.log(res);
        if (res) {
          this.$message.success("添加成功");
          this.dialogFormVisible = false;
          this.load();
        } else {
          this.$message.error("添加失败");
        }
      });
    },
    //修改用户
    updateUser() {
      request.put("/user/updateUser", this.Editusers).then((res) => {
        console.log(res);
        if (res) {
          this.$message.success("修改成功");
          this.dialogFormEditVisible = false;
          this.load();
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    //导出
    exp(){
      window.open("http://localhost:9090/user/export")
    },
    //提示文件导入成功
    handleExcelImportSucess(){
      this.$message.success("导入成功")
      this.load()
    }
  }
};
</script>

<style>
.headerBg {
  background-color: #eee !important;
}
</style>